package com.burgeon.backend.SHOPBACKEND.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.Employee;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/*云仓订单发货单,根据提交状态为1 ，没有快递公司的信息*/
public class QuerryEmployee extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public QuerryEmployee(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }


    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<Employee> getData(String cStoreIdName) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(cStoreIdName), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        ArrayList<Employee> orderArrayList = new ArrayList<Employee>();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);

            Employee employee = new Employee();
            employee.setId(jsonArray.getJSONArray(i).getString(0));
            employee.setName(jsonArray.getJSONArray(i).getString(1));
            employee.setHandset(jsonArray.getJSONArray(i).getString(2));

            orderArrayList.add(employee);
        }
        return orderArrayList;
    }

    private MultiValueMap Data(String cStoreIdName) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("NAME");//快递公司 EB_LOGIS_ID  FASTNO
        columns.add("PHONE");//

        params.put("columns", columns);
        params.put("table", "C_V_EMPLOYEE");//表名

        params.put("count","true");

        HashMap paramss = new HashMap();//DOCNO  (AK)	单据编号ISACTIVE
        HashMap expr1 = new HashMap();
        String ac = "'="+cStoreIdName+"'";
        expr1.put("column", "'C_STORE_ID;NAME'");//11665
        expr1.put("condition", ac);

        HashMap expr2 = new HashMap();
        String is = "'=Y'";
        expr2.put("column","ISACTIVE");
        expr2.put("condition",is);

        paramss.put("combine", "and");//关系
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);

        params.put("params", paramss);
        //System.err.println(params);

        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        QuerryEmployee querryYCRetailout = new QuerryEmployee("39.99.239.133:55322", "nea@burgeon.com.cn", "mgg55321");
        try {
            System.err.println(querryYCRetailout.getData("石家庄长安万达店"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
